from django.db import models
# Create your models here.
import datetime


class Users(models.Model):
    username = models.CharField(max_length=20, unique=True, verbose_name='用户名', null=True)
    password = models.CharField(max_length=30, verbose_name='密码', null=True)
    status_choice = (
        (0, '正常使用'),
        (1, '已禁用')
    )
    status = models.BooleanField(choices=status_choice, default=0, verbose_name='用户状态', null=True, blank=True)
    is_login_choice = status_choice = (
        (0, '未登录'),
        (1, '已登录')
    )
    is_login = models.BooleanField(choices=is_login_choice, default=0, verbose_name='登录状态', null=True, blank=True)
    mail = models.EmailField(null=True, blank=True, verbose_name='邮箱')
    phone = models.CharField(max_length=15, null=True, blank=True, verbose_name='手机号')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间', null=True, blank=True)
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    remark = models.CharField(max_length=256, null=True, blank=True, verbose_name='备注')
    ext = models.JSONField(null=True, blank=True, verbose_name='扩展字段')

    class Meta:
        verbose_name_plural = '用户表'


class Token(models.Model):
    token = models.CharField(unique=True, max_length=200)
    user = models.ForeignKey('Users', on_delete=models.CASCADE)


class ParkingPicture(models.Model):
    file_path = models.FileField(upload_to='parking_picture/', verbose_name='停车位图片链接')

    class Meta:
        verbose_name_plural = '车位图片表'


class ParkingPeriod(models.Model):
    time_start = models.DateTimeField(null=True, verbose_name='车位出租开始时间')
    time_end = models.DateTimeField(null=True, verbose_name='车位出租结束时间')
    parking_space = models.ForeignKey('ParkingSpace', on_delete=models.CASCADE)

    class Meta:
        verbose_name_plural = '可出租时间段表'


class Device(models.Model):
    name = models.CharField(max_length=30, null=True, blank=True, verbose_name='设备名')

    class Meta:
        verbose_name_plural = '设备型号表'


class UserDevice(models.Model):
    number = models.CharField(max_length=30, null=True, blank=True, verbose_name='设备编号', unique=True)
    device = models.ForeignKey('Device', null=True, on_delete=models.CASCADE, verbose_name='设备型号')
    user = models.ForeignKey('Users', null=True, on_delete=models.CASCADE, verbose_name='所属用户')
    use_choice = (
        (True, '已有绑定的车位'),
        (False, '可绑定车位')
    )
    is_use = models.BooleanField(choices=use_choice, verbose_name='是否有绑定的车位', default=0)

    class Meta:
        verbose_name_plural = '车位绑定的设备表'


class ParkingSpace(models.Model):
    device = models.ForeignKey('UserDevice', on_delete=models.CASCADE, null=True, verbose_name='绑定的设备')
    longitude = models.CharField(max_length=20, null=True, blank=True, verbose_name='经度')
    latitude = models.CharField(max_length=20, null=True, blank=True, verbose_name='纬度')
    address = models.CharField(max_length=100, null=False, blank=False, verbose_name='地址备注')
    parking_picture = models.ManyToManyField('ParkingPicture', verbose_name='停车位图片')
    create_time = models.DateTimeField(null=False, auto_now_add=True, verbose_name='车位创建时间')
    introduction = models.TextField(max_length=300, null=True, blank=True, verbose_name='简介')
    user = models.ForeignKey('Users', on_delete=models.CASCADE, verbose_name='出租人')
    ext = models.JSONField(verbose_name='扩展', null=True, blank=True)

    class Meta:
        verbose_name_plural = '车位表'


class Code(models.Model):
    code = models.CharField(max_length=50, unique=True, verbose_name='二维码编号')
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name_plural = '二维码编号表'


class Orders(models.Model):
    car = models.CharField(max_length=64, verbose_name='车牌号')
    user = models.ForeignKey('Users', on_delete=models.DO_NOTHING, verbose_name='用户')
    parking_space = models.ForeignKey('ParkingSpace', on_delete=models.CASCADE, verbose_name='车位')
    status_choice = (
        (0, '未完成'),
        (1, '已完成')
    )
    status = models.BooleanField(choices=status_choice, default=0, verbose_name='订单状态', null=True, blank=True)
    payment = models.DecimalField(max_digits=4, decimal_places=2, default=10.00, verbose_name='金额')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='订单创建时间', null=True, blank=True)
    start_time = models.DateTimeField(auto_now_add=True, verbose_name='租赁开始时间', null=True, blank=True)
    end_time = models.DateTimeField(verbose_name='租赁结束时间')
    code = models.ForeignKey('Code', on_delete=models.CASCADE, verbose_name='二维码编号')

    class Meta:
        verbose_name_plural = '交易记录表'


